package com.zcm.modules.zhaocai.interceptor;

import com.zcm.common.config.Global;
import com.zcm.common.service.BaseService;
import com.zcm.modules.center.utils.CenterUserUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ZcmInterceptor extends BaseService implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {

//		String requestUri = request.getRequestURI();
//		String contextPath = request.getContextPath();
        // 得到访问地址
//		String url = requestUri.substring(contextPath.length());
        /*
         * if (excludeUrls.contains(url)) {// 如果要访问的资源是不需要验证的 return true; }
         */
        // request.setAttribute("msg", "您还没有登录或登录已超时，请重新登录，然后再刷新本功能！");
        // request.getRequestDispatcher(request.getContextPath()+"/nologin.jsp").forward(request,
        // response);
        // response.sendRedirect(request.getContextPath()+Global.getFrontPath()+"/nologin.jsp");

        String userId = CenterUserUtils.getUserId();
        if (userId == null || userId.equalsIgnoreCase("")) { // 没有登陆或者Session超时
            // logger.info( "user not login");
            request.setAttribute("msg", "您还没有登录或登录已超时，请重新登录，然后再刷新本功能！");
            // request.getRequestDispatcher(request.getContextPath()+"/nologin.jsp").forward(request,
            // response);
            // response.sendRedirect(request.getContextPath()+"/login/login.jsp");
            response.sendRedirect(request.getContextPath()
                    + Global.getFrontPath() + "/nologin.jsp");
            return false;
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        if (modelAndView != null) {
            /*
             * // 如果是手机或平板访问的话，则跳转到手机视图页面。
             * if(UserAgentUtils.isMobileOrTablet(request) &&
             * !StringUtils.startsWithIgnoreCase(modelAndView.getViewName(),
             * "redirect:")){ // modelAndView.setViewName("mobile/" +
             * modelAndView.getViewName());
             * modelAndView.setViewName(modelAndView.getViewName()); }
             */
            /*
             * String userId=CenterUserUtils.getUserId(); if(userId==null) {
             * response.sendRedirect(Global.getFrontPath()); }
             */
        }
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
